package com.heatup.apt.api.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.ImmutableMap;
import com.heatup.apt.api.service.UserSendAddrService;
import com.heatup.apt.common.constants.ErrorCodeConstants;
import com.heatup.apt.common.util.JsonTools;
import com.heatup.apt.common.util.RequestTools;
import com.heatup.apt.common.util.ResponseTools;
import com.heatup.apt.model.UserSendAddr;
import com.heatup.apt.model.api.ApiResultModel;
import com.heatup.apt.model.user.UserInfo;

/**
 *
 * UserSendAddr 控制层
 *
 */
@Controller
@RequestMapping("/user_addr")
public class UserSendAddrController extends BaseController{


	@Resource
	private UserSendAddrService userSendAddrService;
	
	/**
	 * 添加用户送餐地址
	 */
	@ResponseBody
	@RequestMapping(value = "add_addr", method={RequestMethod.POST, RequestMethod.GET})
	public String addUserAddr(HttpServletRequest request){
		UserSendAddr userSendAddr = new UserSendAddr();
		String mobile = RequestTools.RequestString(request, "mobile", "");
		String userName = RequestTools.RequestString(request, "userName", "");
		String address = RequestTools.RequestString(request, "address", "");
		String doorNumber = RequestTools.RequestString(request, "doorNumber", "");
		Integer addrId = RequestTools.RequestInt(request, "addrId", 0);
		if(addrId != 0) userSendAddr.setAddrId(addrId);
		userSendAddr.setMobile(mobile);
		userSendAddr.setUserName(userName);
		userSendAddr.setAddress(address);
		userSendAddr.setDoorNumber(doorNumber);
		UserInfo userInfo = this.getUserInfo();
		String callback = RequestTools.RequestString(request, "callback", "");
		ApiResultModel<Object> apiResultModel = new ApiResultModel<>();
		if(userInfo == null || this.getUserId() == null){
			apiResultModel.setErrorCode(ErrorCodeConstants.Code.USER_NOT_LOGIN.getCode());
			apiResultModel.setMsg(ErrorCodeConstants.Code.USER_NOT_LOGIN.getMessage());
			apiResultModel.setRet(apiResultModel.getErrorCode());
			return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel),callback);
		}
		apiResultModel.setLogined(this.isLogined());
		userSendAddr.setUserId(this.getUserId());
		if(addrId != 0) {
			userSendAddrService.updateById(userSendAddr);
			apiResultModel.setMsg("修改成功");
		} else {
			userSendAddrService.insert(userSendAddr);
			apiResultModel.setMsg("添加成功");
		}
		apiResultModel.setRet(ApiResultModel.RET_SUCCESS);
        
		return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel), callback);
	}
	
	/**
	 * 获取用户保存地址
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "get_addr", method={RequestMethod.POST, RequestMethod.GET})
	public String getUserAddr(HttpServletRequest request){
		UserInfo userInfo = this.getUserInfo();
		String callback = RequestTools.RequestString(request, "callback", "");
		ApiResultModel<Object> apiResultModel = new ApiResultModel<>();
		if(userInfo == null || this.getUserId() == null){
			apiResultModel.setErrorCode(ErrorCodeConstants.Code.USER_NOT_LOGIN.getCode());
			apiResultModel.setMsg(ErrorCodeConstants.Code.USER_NOT_LOGIN.getMessage());
			apiResultModel.setRet(apiResultModel.getErrorCode());
			return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel),callback);
		}
		apiResultModel.setLogined(this.isLogined());
		List<UserSendAddr> list = userSendAddrService.selectByMap(ImmutableMap.of("user_id", this.getUserId(), "status", 1));
		apiResultModel.setRet(ApiResultModel.RET_SUCCESS);
		apiResultModel.setData(list);
		return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel), callback);
	}
	
	/**
	 * 获取用户保存地址
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "del_addr", method={RequestMethod.POST, RequestMethod.GET})
	public String delUserAddr(HttpServletRequest request){
		UserInfo userInfo = this.getUserInfo();
		String callback = RequestTools.RequestString(request, "callback", "");
		
		ApiResultModel<Object> apiResultModel = new ApiResultModel<>();
		if(userInfo == null || this.getUserId() == null){
			apiResultModel.setErrorCode(ErrorCodeConstants.Code.USER_NOT_LOGIN.getCode());
			apiResultModel.setMsg(ErrorCodeConstants.Code.USER_NOT_LOGIN.getMessage());
			apiResultModel.setRet(apiResultModel.getErrorCode());
			return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel),callback);
		}
		Integer addrId = RequestTools.RequestInt(request, "addrId", 0);
		apiResultModel.setLogined(this.isLogined());
		UserSendAddr userSendAddr = userSendAddrService.selectById(addrId);
		userSendAddr.setStatus(false);
		boolean res = userSendAddrService.updateById(userSendAddr);
		if(!res){
			apiResultModel.setRet(ApiResultModel.RET_ERROR);
			apiResultModel.setMsg("删除失败");
		}
		List<UserSendAddr> list = userSendAddrService.selectByMap(ImmutableMap.of("user_id", this.getUserId(),"status", 1));
		apiResultModel.setRet(ApiResultModel.RET_SUCCESS);
		apiResultModel.setData(list);
		return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel), callback);
	}
}